// pages/reservation/reservation.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: { 
    orderName: '',
    phoneNum: '', 
    childName: '',
    age: '',
    sex:'男',
    index1: '0',
    index2: '0',
    array: ['体适能', '花式跳绳', '羽毛球'],
    selectArray: ['下周一下午 4：30~6：00', '下周五下午 4：30~6：00', '下周六上午 9：30~11：00', '下周日上午9：30~11：00'],
    showModal: false,
  },

  //改变年龄
  bindchangeSex: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      sex: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {


  },

  //跳转到联系我们
  bindContact: function() {
    wx.navigateTo({
      url: '/pages/contact/contact'
    })
  },
  hideModal: function() {
    this.setData({
      showModal: false
    });

  },

  bindPickerChange: function(e) {
    var array = this.data.array
    console.log('picker发送选择改变，携带值为', array[e.detail.value])
    this.setData({
      index1: e.detail.value
    })
  },
  bindDate: function(e) {
    var selectArray = this.data.selectArray
    console.log('picker发送选择改变，携带值为', selectArray[e.detail.value])
    this.setData({
      index2: e.detail.value
    })
  },

  //预约体验表单提交
  formSubmit: function(e) {
    var me = this;
    console.log('表单e' + JSON.stringify(e));
    var array = this.data.array;
    var selectArray = this.data.selectArray;

    if (e.detail.value.orderName == '') {
      wx.showToast({
        title: '请输入您的姓名!',
        icon: 'none',
        duration: 1500
      })
      setTimeout(function() {
        wx.hideToast()
      }, 2000)
    } else if (!(/^1[34578]\d{9}$/.test(e.detail.value.phoneNum))) {
      wx.showToast({
        title: '请输入正确的手机号码!',
        icon: 'none',
        duration: 1500
      })
      setTimeout(function() {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.childName == '') {
      wx.showToast({
        title: '请输入小宝贝姓名',
        icon: 'none',
        duration: 1500
      })
      // setTimeout(function () {
      //   wx.hideToast()
      // }, 2000)
    } else if (e.detail.value.age == '') {
      wx.showToast({
        title: '请输入小宝贝年龄',
        icon: 'none',
        duration: 1500
      })
    } else if (parseInt(e.detail.value.age) <= 4 || parseInt(e.detail.value.age) > 20) {
      console.log(e.detail.value.age)
      wx.showToast({
        title: '请输入正确的小宝贝年龄',
        icon: 'none',
        duration: 1500
      })
      // setTimeout(function () {
      //   wx.hideToast()
      // }, 2000)
    } else {
      var ordername = e.detail.value.orderName;
      var phonenum = e.detail.value.phoneNum;
      var childname = e.detail.value.childName;
      var age = e.detail.value.age;
      var sex = me.data.sex;
      var orderclass = array[me.data.index1];
      var ordertime = selectArray[me.data.index2];
      console.log('姓名' + ordername + '手机' + phonenum + '宝贝' + childname + '年龄' + age + '性别' + sex + '课程' + orderclass + '时间' + ordertime)
      wx.request({
        url: app.serverUrl + '/orderClass', // 仅为示例，并非真实的接口地址
        method: 'POST',
        data: {
          ordername: ordername,
          phonenum: phonenum,
          childname: childname,
          age: age,
          sex: sex,
          orderclass: orderclass,
          ordertime: ordertime
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          if (res.data.status == 200) {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
            })
            setTimeout(function () {
              wx.hideToast()
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }, 1500)
           
          } else if (res.data.status == 500) {
            wx.showToast({
              title: '提交失败',
              duration: 1500
            })

          }

          console.log(ordername + sex);
        }
      })



      //这里讲数据传给后台

    }

  }
})